package com.diandiansong.app.ui.index;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.OrderIndexInfo;
import com.diandiansong.app.entity.OrderShow;
import com.diandiansong.app.ui.cart.Pay;
import com.diandiansong.app.utils.A;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import com.diandiansong.app.ui.base.BaseAct;
import diandiansong.library.utils.BaseEntity;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

/**
 * 订单详情
 */
public class OederDetail extends BaseAct {

    static OrderIndexInfo.Data mOrder;
    @BindView(R.id.tv_status)
    TextView mTvStatus;
    @BindView(R.id.do_cancel_order)
    TextView mDoCancelOrder;
    @BindView(R.id.do_confirm_receipt)
    TextView mDoConfirmReceipt;
    @BindView(R.id.do_pay)
    TextView mDoPay;
    @BindView(R.id.tv_order_id)
    TextView mTvOrderId;
    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.tv_pirce)
    TextView mTvPirce;
    @BindView(R.id.tv_tr_price)
    TextView mTvTrPrice;
    @BindView(R.id.tv_total_price)
    TextView mTvTotalPrice;
    @BindView(R.id.tv_tr_name)
    TextView mTvTrName;
    @BindView(R.id.tv_tr_phone)
    TextView mTvTrPhone;
    @BindView(R.id.tv_tr_addr)
    TextView mTvTrAddr;
    @BindView(R.id.tv_note)
    TextView mTvNote;
    @BindView(R.id.ll_goods)
    LinearLayout mLlGoods;

    public static void start(Context context, OrderIndexInfo.Data order) {
        mOrder = order;
        Intent starter = new Intent(context, OederDetail.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.order_detail);
        initTitle("订单详情");

        OrderIndexInfo.Data item = mOrder;
        View convertView = mContentView;


        mTvStatus.setText(item.getOrderStatusName());
        mTvOrderId.setText("订单号：" + item.getOrderId());
        mTvDate.setText("下单时间" + item.getCreateTime());

        onLoading();
        A.orderShow(mOrder.getOrderId(), new CCallBack<OrderShow>() {
            @Override
            public void onSuccess(OrderShow o) throws Exception {
                onLoaded();
                OrderShow.Data item = o.getData();
                mTvPirce.setText("商品金额：￥" + item.getGoodsAmount());
                mTvTrPrice.setText("配送费：￥" + item.getCostFreight());
                mTvTotalPrice.setText("订单金额：￥" + item.getOrderAmount());
                mTvTrName.setText("收货人：" + item.getConsignee());
                mTvTrPhone.setText("手机号码：" + item.getConsigneePhone());
                mTvTrAddr.setText("收货地址：" + item.getConsigneeAddress());
                mTvNote.setText(item.getNote());
            }

            @Override
            public void onFailure(int code, String msg) {
                onLoaded();
                toast(msg);
            }
        });


        LinearLayout mLlGoods = convertView.findViewById(R.id.ll_goods);

        List<OrderIndexInfo.Data.Item> items = item.getItem();
        mLlGoods.removeAllViews();
        for (OrderIndexInfo.Data.Item subItem : items) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.order_check_good_item, null);

            ImageView mIvLogo = view.findViewById(R.id.iv_logo);
            TextView mTvName = view.findViewById(R.id.tv_name);
            TextView mTvDes = view.findViewById(R.id.tv_des);
            TextView mTvPrice = view.findViewById(R.id.tv_price);
            TextView mTvCount = view.findViewById(R.id.tv_count);

            ImgLoader.display(mIvLogo, subItem.getItemImg());
            setText(mTvName, subItem.getCommodityName());
            setText(mTvDes, subItem.getSpecInfo());
            setText(mTvPrice, "￥" + subItem.getPrice() + subItem.getUnit());
            setText(mTvCount, "x" + subItem.getNumber());

            mLlGoods.addView(view);
        }
        //0=待支付 1=待发货 2=待收货 3=已收货 4=已完成 6=取消订单
        if (item.getOrderStatus().equals("0")) {//0=待支付 》》取消/支付
            mDoCancelOrder.setVisibility(View.VISIBLE);
            mDoPay.setVisibility(View.VISIBLE);
            mDoConfirmReceipt.setVisibility(View.GONE);

        } else if (item.getOrderStatus().equals("1")) {//1=待发货 》》无
            mDoCancelOrder.setVisibility(View.VISIBLE );
            mDoPay.setVisibility(View.GONE);
            mDoConfirmReceipt.setVisibility(View.GONE);

        } else if (item.getOrderStatus().equals("2")) {//2=待收货 》》 确认收货
            mDoCancelOrder.setVisibility(View.GONE);
            mDoPay.setVisibility(View.GONE);
            mDoConfirmReceipt.setVisibility(View.VISIBLE);

        } else {//3=已收货 4=已完成 》》无
            mDoCancelOrder.setVisibility(View.GONE);
            mDoPay.setVisibility(View.GONE);
            mDoConfirmReceipt.setVisibility(View.GONE);
        }


        //其他信息
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.do_cancel_order, R.id.do_confirm_receipt, R.id.do_pay})
    public void onViewClicked(View view) {
        OrderIndexInfo.Data item = mOrder;
        switch (view.getId()) {
            case R.id.do_cancel_order: {
                onLoading();
                A.orderCancel(item.getOrderId(), new CCallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity o) throws Exception {
                        onLoaded();
                        doUpdateStatus("已取消");
                        Order.SubFragment.doRefresh();

                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        onLoaded();
                        toast(msg);
                    }
                });
            }
            break;
            case R.id.do_confirm_receipt: {
                onLoading();
                A.orderReceipt(item.getOrderId(), new CCallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity baseEntity) throws Exception {
                        onLoaded();
                        doUpdateStatus("已收货");
                        Order.SubFragment.doRefresh();
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        onLoaded();
                        toast(msg);
                    }
                });
            }
            break;
            case R.id.do_pay: {
                Pay.start(getActivity(), item.getOrderId());
            }
            break;
        }
    }

    public static class Status {
        public String status;

        public Status(String status) {
            this.status = status;
        }
    }

    public static void doUpdateStatus(String status) {
        EventBus.getDefault().post(new Status(status));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveStatus(Status status) {
        if (mDoCancelOrder == null || mDoPay == null || mDoConfirmReceipt == null || mTvStatus == null) {
            return;
        }

        mDoCancelOrder.setVisibility(View.GONE);
        mDoPay.setVisibility(View.GONE);
        mDoConfirmReceipt.setVisibility(View.GONE);

        mTvStatus.setText(status.status);
    }
}
